package leetcode.editor.algorithms;

import org.junit.Test;

import java.util.Arrays;

/**
 * 二分查找
 *
 */
@SuppressWarnings("all")
public class BinarySearch {

    private static final Integer i=128;
    public static void main(String[] args) {
         int [] arr = {1,2,3,5,6,7,8,9,10};
         Arrays.sort(arr);
         new BinarySearch().binarySearch(arr,9);
        Integer integer = new BinarySearch().binarySearch(arr, 8, 0, arr.length - 1);
        System.out.println(integer);
        Integer j=128;
        System.out.println(i>=j);
    }

    /**
     * 双指针实现二分查找
     * @param arr
     * @param temp
     * @return
     */
    public Integer binarySearch(int[] arr,int temp) {
        //n*n
        int left =0;
        int right = arr.length-1;
        if (temp<arr[0]||temp>arr[arr.length-1])
            return -1;
        while (left<=right) {
            int mid =(left+right)/2;
            if (temp==arr[mid]) {
                return mid;
            }
            if (arr[mid]>temp) {
                right =mid;
            }
            if (arr[mid]<temp) {
                left = mid;
            }
        }
        return  -1;
    }

    /**
     * 递归实现二分查找
     * 时间复杂度 log n    空间复杂度  log n
     * @param arr
     * @param temp
     * @param start
     * @param end
     * @return
     */
    public Integer binarySearch(int[] arr,int temp,int start,int end) {
        if (temp < arr[start] || temp > arr[end] || start > end) {
            return -1;
        }
        //计算中间位置
        int mid = (start + end) / 2;
        if (arr[mid]>temp) {
            return binarySearch(arr,temp,start,mid-1);
        }
        else if (arr[mid]<temp) {
            return binarySearch(arr,temp,mid+1,end);
        } else {
            return mid;
        }
    }

    @Test
    public void test1() {
        int i=2;
        i=i++;
//        int j;
//        j=i++;
//        System.out.println(j);
        System.out.println(i);
    }
    @Test
    public void test2() {
        Integer i =128;
        Integer j =2;
        System.out.println(i==j);
        System.out.println(i==128);
        Integer i1 =128434;
        Integer j1= 128434;
        System.out.println(i1==j1);

        System.out.println(i1.equals(j1));

        Long long1=1232l;
        Long long2=1232l;
        System.out.println(long1==long2);

        String s = new String("hello") + new String("world");
        s.intern();
        String s1 ="helloworld";
        System.out.println(s == s1);
    }
    @Test
    public void test3() {

    }
}
